package pers.mihao.quickstream.common.task.domain.scheduler;

import lombok.*;
import pers.mihao.quickstream.common.task.scheduler.TaskInvokeRes;

import java.util.Date;

/**
 * 指定时间 执行失败执行多次
 *
 * @author mihao
 * @since 2024/11/15
 */
@Data
public class InvokeFailRetryTaskTimer implements AutomaticTaskTimer {

    private Long nextTime;

    private Integer failRetryNum;

    public InvokeFailRetryTaskTimer() {
    }

    public InvokeFailRetryTaskTimer(Long nextTime, Integer failRetryNum) {
        this.nextTime = nextTime;
        this.failRetryNum = failRetryNum;
    }

    @Override
    public Date getNextInvokeTime(TaskInvokeRes lastTaskInvokeRes, int invokeNum) {
        if (lastTaskInvokeRes == null) {
            return new Date(nextTime);
        } else if (!Boolean.TRUE.equals(lastTaskInvokeRes.getInvokerRes()) && invokeNum < failRetryNum) {
            return new Date(System.currentTimeMillis() + invokeNum * 1000L);
        }
        return null;
    }

    @Override
    public String type() {
        return "INVOKE_ONCE";
    }
}
